/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sfs2x.extension.fourinarow.main;

import com.athena.objects.Game;
import com.athena.objects.Player;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

/**
 *
 * @author dodinhchien
 */
public class DisconnectEventHandle extends BaseServerEventHandler{

    @Override
    public void handleServerEvent(ISFSEvent e) throws SFSException {
        User user = (User) e.getParameter(SFSEventParam.USER);
        FourinarowExtension extension = (FourinarowExtension) getParentExtension();
        if(extension.getUsersList().containsKey(user.getId())){
            Player player = extension.getUsersList().get(user.getId());
            if(player != null){
                if(player.getRoomId() != -1){
                    Game game = extension.getGamesList().get(player.getRoomId());
                    trace(game.isStarted());
                    if(game.isStarted()){
                        player.setDisconnect(true);
                    }else{
                        game.getPlayers().remove(player.getId());
                    }
                    
                }
            }
            extension.getUsersList().remove(user.getId());
        }
            
    }
    
}
